<?php
class MicroTal_Builder {
	const M_RAW = 1;
	const M_ECHO = 2;
	const M_PHP = 3;
    protected $begins;
	protected $ends;
	protected $content;
	function __construct($content = '', $begins = self::M_RAW, $ends = self::M_RAW) {
		$this->content = $content;
		$this->begins = $begins;
		$this->ends = $ends;
	}
	function appendRaw($string) {
		switch($this->ends) {
			case self::M_RAW:
				break;
			case self::M_PHP:
				$this->content .= ' ?>';
				break;
			case self::M_ECHO:
				$this->content .= ' ?>';
				break;
		}
		$this->content .= $string;
		$this->ends = self::M_RAW;
	}
	function appendEcho($string) {
		switch($this->ends) {
			case self::M_RAW:
				$this->content .= '<?php echo ';
				break;
			case self::M_PHP:
				$this->content .= 'echo ';
				break;
			case self::M_ECHO:
				$this->content .= '.';
				break;
		}
		$this->content .= "'".str_replace("'", "\'", $string)."'";
		$this->ends = self::M_ECHO;
	}
	function appendPhp($string) {
		switch($this->ends) {
			case self::M_RAW:
				$this->content .= '<?php ';
				break;
			case self::M_PHP:
				break;
			case self::M_ECHO:
				$this->content .= ";\n";
				break;
		}
		$this->content .= $string.";\n";
		$this->ends = self::M_PHP;
	}
	protected function prepend($string) {
		$this->content = $string . $this->content;
	}
	function prependPhp($string) {
		switch($this->begins) {
			case self::M_RAW:
				$this->prepend($string.";\n ?>");
				break;
			case self::M_PHP:
				$this->prepend($string.";\n");
				break;
			case self::M_ECHO:
				$this->prepend($string.";\n echo ");
				break;
		}
		$this->begins = self::M_PHP;
	}
	function prependRaw($string) {
		switch($this->begins) {
			case self::M_RAW:
				$this->prepend($string);
				break;
			case self::M_PHP:
				$this->prepend($string."<?php ");
				break;
			case self::M_ECHO:
				$this->prepend($string."<?php echo ");
				break;
		}
		$this->begins = self::M_RAW;
	}
	function prependEcho($string) {
		$string = "'".str_replace("'", "\'", $string)."'";
		switch($this->begins) {
			case self::M_RAW:
				$this->prepend($string.";\n ?>");
				break;
			case self::M_PHP:
				$this->prepend($string.";\n ");
				break;
			case self::M_ECHO:
				$this->prepend($string.'.');
				break;
		}
		$this->begins = self::M_ECHO;
	}
	function  __toString() {
		$this->appendRaw('');
		$this->prependRaw('');
		return $this->content;
	}
	function  forEval() {
		$this->appendPhp('');
		$this->prependPhp('');
		return $this->content;
	}
}


